package com.zheng.upms.rpc.service.impl;

import com.github.pagehelper.util.StringUtil;
import com.zheng.common.annotation.BaseService;
import com.zheng.common.base.BaseServiceImpl;
import com.zheng.common.util.RedisUtil;
import com.zheng.upms.common.constant.UpmsConstant;
import com.zheng.upms.dao.mapper.UpmsSystemParamMapper;
import com.zheng.upms.dao.model.UpmsDict;
import com.zheng.upms.dao.model.UpmsDictExample;
import com.zheng.upms.dao.model.UpmsSystemParam;
import com.zheng.upms.dao.model.UpmsSystemParamExample;
import com.zheng.upms.rpc.api.UpmsSystemParamService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.Jedis;

import java.util.List;

/**
* UpmsSystemParamService实现
* Created by shuzheng on 2018/10/12.
*/
@Service
@Transactional
@BaseService
public class UpmsSystemParamServiceImpl extends BaseServiceImpl<UpmsSystemParamMapper, UpmsSystemParam, UpmsSystemParamExample> implements UpmsSystemParamService {

    private static final Logger LOGGER = LoggerFactory.getLogger(UpmsSystemParamServiceImpl.class);

    @Autowired
    UpmsSystemParamMapper upmsSystemParamMapper;

    @Override
    public String querySystemParamValueByParam_key(String param_key) {

        String redisKey = UpmsConstant.REDIS_UPMS_SYSTEM_PARAM_PREFIX + ":" + param_key;
        // 从缓存中获取参数的值，如果没有该值，则从数据库中获取，最后再存入redis中

        //Jedis jedis = RedisUtil.getJedis();
        String redisDdvalue = RedisUtil.get(redisKey);
        if (StringUtil.isNotEmpty(redisDdvalue)) {
            return redisDdvalue;
        }

        UpmsSystemParamExample upmsSystemParamExample = new UpmsSystemParamExample();
        UpmsSystemParamExample.Criteria upmsSystemCriteria = upmsSystemParamExample.createCriteria();
        upmsSystemCriteria.andParamKeyEqualTo(param_key);
        List<UpmsSystemParam> list = upmsSystemParamMapper.selectByExample(upmsSystemParamExample);
        if (list != null && list.size() > 0) {
            UpmsSystemParam dd = (UpmsSystemParam)list.get(0);

            String ddvalue = dd.getParamValue();
            // 在Redis中设置数据字典的值
            RedisUtil.set(redisKey, ddvalue);

            return ddvalue;
        }

        return "";
    }

    @Override
    public int cacherefresh() {
        UpmsSystemParamExample upmsSystemParamExample = new UpmsSystemParamExample();
        UpmsSystemParamExample.Criteria upmsSystemCriteria = upmsSystemParamExample.createCriteria();
        List<UpmsSystemParam> list = upmsSystemParamMapper.selectByExample(upmsSystemParamExample);
        if(list!=null){
            for(int i=0;i< list.size();i++) {
                UpmsSystemParam upmsSystemParam = (UpmsSystemParam)list.get(i);
                String redisKey = UpmsConstant.REDIS_UPMS_SYSTEM_PARAM_PREFIX + ":" + upmsSystemParam.getParamKey();
                RedisUtil.remove(redisKey);
            }
        }
        return 1;
    }
}